package com.hongdee.insurance.contract.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hongdee.common.util.StringUtil;
import com.hongdee.insurance.contract.mapper.TInsuranceCompanyContractMapper;
import com.hongdee.insurance.contract.pojo.TInsuranceCompanyContractPojo;
import com.hongdee.insurance.contract.service.ITInsuranceCompanyContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Liu XiaoLong
 * Date: 2017/11/10.
 * Remark:
 */
@Service
public class TInsuranceCompanyContractServiceImpl implements ITInsuranceCompanyContractService {

    @Autowired
    private TInsuranceCompanyContractMapper tInsuranceCompanyContractMapper;
    /**
     * @Name: 分页查询
     * @Author: Liu XiaoLong
     * @Date: 2017/11/10 15:43
     * @Remark:
     */
    @Override
    public Map page(String uid, Map map,int page,int limit) throws Exception {

        PageHelper.startPage(page,limit);
        List list = tInsuranceCompanyContractMapper.list(map);
        PageInfo<TInsuranceCompanyContractPojo> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        Map resMap = new HashMap();
        resMap.put("dataList",list);
        resMap.put("total",total);
        return resMap;
    }

    /**
     * @Name: 更新
     * @Author: Liu XiaoLong
     * @Date: 2017/11/10 17:01
     * @Remark:
     */
    @Override
    public Map update(String uid, Map map) throws Exception {

        String mArea = map.get("area").toString(); // 区域
        String mInsuranceCompany = map.get("insuranceCompany").toString(); // 保险公司
        String mYear = map.get("year").toString(); // 年份
        String mCommonType = map.get("commonType").toString(); // 应用类型
        String mType = map.get("type").toString(); // 保险类型
        String contractNo = map.get("contractNo").toString(); // 合同号
        String mAreas = map.get("oAreas").toString(); // 区县

        if(mCommonType.equals("1")){ // 如果设置通用
            List respList2 = tInsuranceCompanyContractMapper.getListOnArea(mCommonType,mArea,mInsuranceCompany,mYear,mType);
            if(respList2.size()>0){
                throw new Exception("同一区域下只允许有一个通用合同号！");
            }
        }




        List respList = tInsuranceCompanyContractMapper.getContractNoList(contractNo,mArea,mInsuranceCompany,mYear,mCommonType,mType,mAreas);
        if(respList.size()>0){
            throw new Exception("合同号已存在！");
        }



        if(!map.get("id").toString().isEmpty()){
            TInsuranceCompanyContractPojo insuranceCompanyContractPojo = new TInsuranceCompanyContractPojo();
            insuranceCompanyContractPojo.setId(map.get("id").toString());
            insuranceCompanyContractPojo.setStatus("1");
            insuranceCompanyContractPojo.setArea(map.get("area").toString());
            insuranceCompanyContractPojo.setCity(map.get("oCity").toString());
            insuranceCompanyContractPojo.setProvince(map.get("oProvance").toString());
            insuranceCompanyContractPojo.setYear(map.get("year").toString());
            insuranceCompanyContractPojo.setInsuranceCompanyId(map.get("insuranceCompany").toString());
            insuranceCompanyContractPojo.setType(map.get("type").toString());
            insuranceCompanyContractPojo.setCommonType(map.get("commonType").toString());
            insuranceCompanyContractPojo.setCommonType(map.get("lengthNumber").toString());//"lengthNumber" -> "111"
            insuranceCompanyContractPojo.setContractNo(map.get("contractNo").toString());
            insuranceCompanyContractPojo.setAreas(map.get("oAreas").toString());

            tInsuranceCompanyContractMapper.UpdatePoJo(insuranceCompanyContractPojo);
        }
        else {
            TInsuranceCompanyContractPojo insuranceCompanyContractPojo = new TInsuranceCompanyContractPojo();
            insuranceCompanyContractPojo.setId(StringUtil.UUID());
            insuranceCompanyContractPojo.setStatus("1");
            insuranceCompanyContractPojo.setArea(map.get("area").toString());
            insuranceCompanyContractPojo.setCity(map.get("oCity").toString());
            insuranceCompanyContractPojo.setProvince(map.get("oProvance").toString());
            insuranceCompanyContractPojo.setYear(map.get("year").toString());
            insuranceCompanyContractPojo.setInsuranceCompanyId(map.get("insuranceCompany").toString());
            insuranceCompanyContractPojo.setType(map.get("type").toString());
            insuranceCompanyContractPojo.setCommonType(map.get("commonType").toString());
            insuranceCompanyContractPojo.setLengthNumber(map.get("lengthNumber").toString());
            insuranceCompanyContractPojo.setContractNo(map.get("contractNo").toString());
            insuranceCompanyContractPojo.setAreas(map.get("oAreas").toString());
            tInsuranceCompanyContractMapper.Insert(insuranceCompanyContractPojo);
        }
       return new HashMap();
    }

    @Override
    public Map start(String uid,String id) throws Exception {
        tInsuranceCompanyContractMapper.updateStatus("1",id);

        return  new HashMap();
    }

    @Override
    public Map stop(String uid,String id) throws Exception {
        tInsuranceCompanyContractMapper.updateStatus("0",id);

        return  new HashMap();
    }

    @Override
    public Map info(String uid, String id) throws Exception {
        return null;
    }
}
